#include "baseband.h"

void reset_agc(){
    u32 reg_agc_cmd_val = gw_ReadReg(AGC_CMD_ADDRESS);
    reg_agc_cmd_val = reg_agc_cmd_val | 0x00004000;
    gw_WriteReg(AGC_CMD_ADDRESS, reg_agc_cmd_val);
    // gw_sleep(DELAY_WR);
    // reg_agc_cmd_val = gw_ReadReg(AGC_CMD_ADDRESS);
}

void agc_check(State *p_xState){
    // 初始化，call 一次    
    if(p_xState->agc_state.time_cnt == 0){
        p_xState->agc_state.reg_sync_val = gw_ReadReg(RX_SYNC_ADDRESS);
        p_xState->agc_state.time_cnt = AGC_RESET_TIME_S;
        return;
    }

    p_xState->agc_state.time_cnt = p_xState->agc_state.time_cnt - 1;
    if(p_xState->agc_state.time_cnt == 1){
        // 检查寄存器，并比较
        u32 current_reg_sync_val = gw_ReadReg(RX_SYNC_ADDRESS);
        if(p_xState->agc_state.reg_sync_val == current_reg_sync_val){
            reset_agc();
            p_xState->agc_state.reset_num = p_xState->agc_state.reset_num + 1;
        }

        p_xState->agc_state.reg_sync_val = current_reg_sync_val;
        p_xState->agc_state.time_cnt = AGC_RESET_TIME_S;
    }
}
